package com.project.controller;

import com.project.bean.SellCarBean;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;

@RestController
@RequestMapping("sellCar")
public class SellCarController {
    @Autowired
    private ISellCarService service;
@RequestMapping("sell")
    public Collection<SellCarBean> sell(Integer productId, HttpServletRequest request){
        Map<Integer,SellCarBean> sellCarMap = (Map<Integer, SellCarBean>) request.getSession().getAttribute("sellCarMap");
        if(productId != null){
            service.sell(productId, sellCarMap);
        }
        return sellCarMap.values();
    }

    @RequestMapping("remove")
    public String remove(Integer productId,HttpServletRequest request){
        Map<Integer,SellCarBean> sellCarMap =
            (Map<Integer, SellCarBean>) request.getSession().getAttribute("sellCarMap");
        service.remove(productId, sellCarMap);
        return "ok";
    }

    public String updateNumber(Integer productId,int num, HttpServletRequest request){
        Map<Integer,SellCarBean> sellCarMap =
                (Map<Integer, SellCarBean>) request.getSession().getAttribute("sellCarMap");
        service.updateNum(productId, sellCarMap,num);
        return "ok";
    }

}
